<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
    <title>校园风采管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
        }
        .top-bar {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .search-box {
            display: flex;
            align-items: center;
        }
        .search-box input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            width: 200px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
        }
        .btn-primary {
            background-color: #1ab394;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        .btn-warning {
            background-color: #f8ac59;
        }
        .btn-warning:hover {
            background-color: #f7a54a;
        }
        .btn-danger {
            background-color: #ed5565;
        }
        .btn-danger:hover {
            background-color: #ec4758;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e7eaec;
        }
        th {
            background-color: #f5f5f6;
            color: #333;
        }
        .status-badge {
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 12px;
            color: white;
        }
        .status-active {
            background-color: #1ab394;
        }
        .status-inactive {
            background-color: #ed5565;
        }
        .thumbnail {
            max-width: 100px;
            max-height: 60px;
            object-fit: cover;
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            background-color: white;
            cursor: pointer;
            border-radius: 3px;
        }
        .pagination button:hover:not(:disabled) {
            background-color: #f5f5f5;
            color:#1ab394;
        }
        .pagination button.active {
            background-color: #1ab394;
            color: white;
            border-color: #1ab394;
        }
        .pagination button:disabled {
            background-color: #f5f5f5;
            cursor: not-allowed;
        }
        .pagination-info {
            margin: 0 15px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="top-bar">
            <button class="btn btn-primary" onclick="location.href='${pageContext.request.contextPath}/admin/campus/get'">
                <i class="fas fa-plus"></i> 添加校园风采
            </button>
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="输入标题搜索" value="${title}">
                <button class="btn btn-primary" onclick="search()">
                    <i class="fas fa-search"></i> 搜索
                </button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>图片</th>
                    <th>标题</th>
                    <th>状态</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${styleList}" var="style">
                    <tr>
                        <td>${style.styleId}</td>
                        <td>
                            <c:if test="${not empty style.imageUrl}">
                                <img src="${pageContext.request.contextPath}/${style.imageUrl}" 
                                     alt="${style.title}" class="thumbnail">
                            </c:if>
                        </td>
                        <td>${style.title}</td>
                        <td>
                            <span class="status-badge ${style.status == 1 ? 'status-active' : 'status-inactive'}">
                                ${style.status == 1 ? '显示' : '隐藏'}
                            </span>
                        </td>
                        <td><fmt:formatDate value="${style.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                        <td>
                            <button class="btn btn-warning" onclick="location.href='${pageContext.request.contextPath}/admin/campus/get?id=${style.styleId}'">
                                <i class="fas fa-edit"></i> 编辑
                            </button>
                            <button class="btn btn-danger" onclick="deleteStyle(${style.styleId})">
                                <i class="fas fa-trash"></i> 删除
                            </button>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>

        <div class="pagination">
            <button onclick="goToPage(${currentPage - 1})" ${currentPage <= 1 ? 'disabled' : ''}>
                <i class="fas fa-chevron-left"></i> 上一页
            </button>
            
            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == currentPage}">
                        <button class="active">${i}</button>
                    </c:when>
                    <c:otherwise>
                        <button onclick="goToPage(${i})">${i}</button>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            
            <button onclick="goToPage(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
                下一页 <i class="fas fa-chevron-right"></i>
            </button>
            
            <span class="pagination-info">
                共 ${totalCount} 条记录，${totalPages} 页
            </span>
        </div>
    </div>

    <script>
        function deleteStyle(id) {
            if (confirm('确定要删除这条校园风采吗？')) {
                fetch('${pageContext.request.contextPath}/admin/campus/delete?id=' + id, {
                    method: 'POST'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        location.reload();
                    } else {
                        alert(data.message || '删除失败');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败');
                });
            }
        }

        function search() {
            const searchValue = document.getElementById('searchInput').value.trim();
            location.href = '${pageContext.request.contextPath}/admin/campus/list?title=' + 
                          encodeURIComponent(searchValue);
        }

        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                search();
            }
        });

        function goToPage(page) {
            const searchValue = document.getElementById('searchInput').value.trim();
            const searchParam = searchValue ? '&title=' + encodeURIComponent(searchValue) : '';
            location.href = '${pageContext.request.contextPath}/admin/campus/list?page=' + page + searchParam;
        }
    </script>
</body>
</html> 